<template>
    <div>
        <border-header second="商品管理" third="商品列表"></border-header>

        <el-card>
            <el-row :gutter="20">
                <el-col :span="8">
                     <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getGoodsList">
                        <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
                    </el-input>
                </el-col>
                <el-col :span="4">
                    <el-button type="primary" @click="goAddpage">添加商品</el-button>
                </el-col>
            </el-row>

            <!-- 商品列表主体 -->
            <el-table :data="goodsList"  style="width: 100%" stripe border>
                <el-table-column type="index"></el-table-column>
                <el-table-column prop="goods_name" label="商品名称" width="400"></el-table-column>
                <el-table-column prop="goods_price" label="商品价格(元)"></el-table-column>
                <el-table-column prop="goods_weight" label="商品重量"></el-table-column>
                <el-table-column prop="add_time" label="创建时间"></el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <el-button type="primary" icon="el-icon-edit" size="mini"></el-button>
                        <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeById(scope.row.goods_id)"></el-button>
                    </template>
                </el-table-column>
            </el-table>

            <!-- 分页器 -->
            <el-pagination @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="queryInfo.pagenum"
            :page-sizes="[10, 20, 30, 40]"
            :page-size="queryInfo.pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
            </el-pagination>
        </el-card>
    </div>
</template>
<script>
import BorderHeader from '../BorderHeader.vue'
import http from '@/util/http'
import moment from 'moment'
export default {
  data () {
    return {
      // 获取商品列表数据的参数对象
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      total: 0,
      // 商品列表数据
      goodsList: []
    }
  },
  components: {
    BorderHeader
  },
  created () {
    this.getGoodsList()
  },
  methods: {
    // 获取商品列表数据
    async getGoodsList () {
      const { data: res } = await http.get('goods', { params: this.queryInfo })
      if (res.meta.status !== 200) return this.$message.error('获取商品列表数据失败')

      // 在赋值之前，对时间进行格式化操作(使用moment时间插件)
      res.data.goods.forEach(item => {
        item.add_time = moment(item.add_time).format('YYYY-MM-DD')
      })

      this.goodsList = res.data.goods
      this.total = res.data.total
    },
    // 页码大小发生改变时的函数
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.getGoodsList()
    },
    // 当前页发生改变时的函数
    handleCurrentChange (newPage) {
      this.queryInfo.pagenum = newPage
      this.getGoodsList()
    },
    // 点击删除按钮发生的事件处理函数
    async removeById (id) {
      const confiemResult = await this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      // 如果用户确认删除，则返回值是confirm
      if (confiemResult !== 'confirm') {
        return this.$message.info('已取消删除')
      }
      const { data: res } = await http.delete('goods/' + id)
      if (res.meta.status !== 200) return this.$message.error('删除商品失败')
      this.getGoodsList()
      this.$message.success('删除商品成功')
    },
    // 点击添加商品按钮
    goAddpage () {
      this.$router.push('/goods/add')
    }
  }
}
</script>
